package framework.objects;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

public abstract class GameObjectsGroup<T extends GameObject> implements Drawable {

	protected Object[] gameObjects;
	protected int capacity;
	
	public GameObjectsGroup(int capacity) {
		this.capacity = capacity;
		this.gameObjects = new Object[capacity];
	}
	
	public void add(T gameObject) {
		for (int i = 0; i < capacity; i++) {
			if (gameObjects[i] == null) {
				gameObjects[i] = gameObject;
				return;
			}
		}
		Log.e("Group", "Out of capacity, can't add an object.");
	}
	
	public void remove(T gameObject) {
		for (int i = 0; i < capacity; i++) {
			if (gameObjects[i] == gameObject) {
				gameObjects[i] = null;
				return;
			}
		}
	}
	
	public void removeAt(int index) {
		gameObjects[index] = null;
	}
	
	@SuppressWarnings("unchecked")
	public void draw(GL10 gl, float deltaTime) {
		for (int i = 0; i < capacity; i++) {
			if (gameObjects[i] != null) {
				((T)gameObjects[i]).draw(gl, deltaTime);
			}
		}			
	}
	
	public Object[] getArray() {
		return gameObjects;
	}
	
}
